package com.hermit.qmst.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.hermit.qmst.R;
import com.hermit.qmst.UI.activity.CallingActivity;
import com.hermit.qmst.UI.activity.SelectCallActivity;
import com.hermit.qmst.mode.ContactInfo;
import com.hermit.qmst.tools.Common;
import com.hermit.qmst.tools.Preferences;
import com.hermit.qmst.tools.SipCallHelp;
import com.hermit.qmst.tools.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hermithermit on 15/2/14.
 */
public class ContactAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<ContactInfo> mContactInfoList;
    private boolean isShowLetter = true;
    private String TAG = "ContactAdapter";
    private SelectBox mSelectBox;
    private int mseleIndex=-1;
    public Map<Integer,Boolean> map=new HashMap<>();
    public List<String> phones=new ArrayList<>();
    public ContactAdapter(Context context, ArrayList<ContactInfo> list) {
        this.mContext = context;
        this.mContactInfoList = list;
    }
    private boolean isMain=true;

    public void setMain(boolean main) {
        isMain = main;
    }

    public void setSelectBox(SelectBox selectBox) {
        this.mSelectBox = selectBox;
    }

    public ContactAdapter(Context context, ArrayList<ContactInfo> list, boolean isShowLetter) {
        this.mContext = context;
        this.mContactInfoList = list;
        this.isShowLetter = isShowLetter;
    }

    @Override
    public int getCount() {
        return this.mContactInfoList.size();
    }

    @Override
    public Object getItem(int position) {
        return this.mContactInfoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        Holder holder = null;

        if (convertView == null) {
            holder = new Holder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_list_contact, null);

            holder.tvName = (TextView) convertView.findViewById(R.id.tv_contact_name);
            holder.tvPhone = (TextView) convertView.findViewById(R.id.tv_contact_phone);
            holder.tvArea = (TextView) convertView.findViewById(R.id.tv_contact_area);
            holder.tvLetter = (TextView) convertView.findViewById(R.id.tv_letter);
            holder.lineLetter = (View) convertView.findViewById(R.id.line_letter);
            holder.btCallItem = (ImageView) convertView.findViewById(R.id.bt_call_item);
            holder.cb_check = (CheckBox) convertView.findViewById(R.id.cb_check);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        if(isMain){
            holder.cb_check.setId(position);
            holder.cb_check.setOnCheckedChangeListener(mSelectBox);
            holder.cb_check.setVisibility(View.VISIBLE);
        }else{
            holder.cb_check.setVisibility(View.INVISIBLE);
        }
        final ContactInfo info = mContactInfoList.get(position);
        holder.lineLetter.setVisibility(View.GONE);
        holder.tvLetter.setVisibility(View.GONE);


        if (isShowLetter) {
            //当前联系人的首字母
            String curLetter = Utils.getFirstLetter(info.getSortKey());
            if (position == 0) {
                holder.lineLetter.setVisibility(View.VISIBLE);
                holder.tvLetter.setVisibility(View.VISIBLE);
                holder.tvLetter.setText(curLetter);
            } else {

                ContactInfo preInfo = mContactInfoList.get(position - 1);
                String preLetter = Utils.getFirstLetter(preInfo.getSortKey());
                if (!preLetter.equals(curLetter)) {
                    holder.lineLetter.setVisibility(View.VISIBLE);
                    holder.tvLetter.setVisibility(View.VISIBLE);
                    holder.tvLetter.setText(curLetter);
                }

            }
        }

        if (info.isShowCallItem()) {
            holder.btCallItem.setVisibility(View.VISIBLE);
        } else {
            holder.btCallItem.setVisibility(View.GONE);
        }

        holder.tvName.setText(info.getName());
        holder.tvPhone.setText(info.getPhone1());
        if(map!=null&&map.containsKey(position)){
            holder.cb_check.setChecked(true);
        }else {
            holder.cb_check.setChecked(false);

        }

        holder.btCallItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Common.mCallPhone = info.getPhone1();
                Common.mCallAera = info.getPhoneAera();
                Common.mCallName = info.getName();

                Preferences p = new Preferences(mContext);
                String type = p.getPreferenceStringValue(Preferences.CALL_WAY);

                if (type.equals("0")) {//回拨
                    Intent intent = new Intent(mContext, CallingActivity.class);
                    mContext.startActivity(intent);

                } else if (type.equals("1")) {//直拨
                    SipCallHelp.placeCallWithOption(null, mContext, info.getPhone1());
                } else if (type.equals("2")) {//智能拨打
                    if (Common.checkWIFI(mContext)) {
                        SipCallHelp.placeCallWithOption(null, mContext, info.getPhone1());
                    } else {
                        Intent intent = new Intent(mContext, CallingActivity.class);
                        mContext.startActivity(intent);

                    }
                } else {
                    Intent intent = new Intent(mContext, SelectCallActivity.class);
                    mContext.startActivity(intent);
                }
            }
        });


        return convertView;
    }

    private class Holder {
        private TextView tvName;
        private TextView tvPhone;
        private TextView tvArea;
        private TextView tvLetter;
        private View lineLetter;
        private ImageView btCallItem;
        private CheckBox cb_check;
    }

    public static abstract class SelectBox implements CompoundButton.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            myOnCheckedChanged(buttonView.getId(), isChecked);
        }

        public abstract void myOnCheckedChanged(int positon, boolean isChecked);
    }
}
